package main.parser;

import java.util.List;
import main.parser.FlowParser;
import main.flow.Flow;

public class LineParser 
{
	private String[] params;
	private Float time;
	private List<Flow> flows;
	
	public LineParser(List<Flow> _flows)
	{
		flows = _flows;
	}
	
	public boolean parse(String line)
	{
		params = line.trim().split("[\\[\\]\\(\\)]");
		
		if (params.length < 4) return false;
		if (!checkHeader()) return false;
		
		int no = 0;
		FlowParser flow = new FlowParser(flows, time);
		
		for (String param : params)
		{
			if (flow.parse(param)) no++;
		}
		
		return (no > 0);
	}
	
	public Float getTime()
	{
		return time;
	}
	
	private boolean checkHeader()
	{
		if (!params[2].trim().startsWith("OPNEX:")) return false;
		
		try
		{
			time = new Float(params[1]); 
		}
		catch (NumberFormatException e)
		{
			return false;
		}
		
		return true;
	}
}
